// pages/guidance/guidance.js
var app = getApp();
var _PAGESIZE = 10;//默认页量
var _PAGE = 1;//默认页码
var load = true;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    code: '',//户号
    checkit:false,
    chooseDate:'',//当前选中的年月
    endDate:'',//最大日期本月
    showMoreIndex: -1,//展开更多的index

    dataList: [],
    hasMore: false, //数据是否加载完成
    loading: false,
    load_show: false,//整体加一个开关 加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.code){
      this.setData({
        code: options.code
      })
    }
    var date = new Date();
    var Year = date.getFullYear(); 
    var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
    this.setData({
      endDate: Year + '-' + month,
      chooseDate: Year + '-' + month,
      dataList: []
    })
    console.log('当前年月', this.data.endDate)
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    _PAGE = 1;
    that.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    _PAGE++;
    that.getList();
  },
  //radio点击事件
  radioChange (e){
      console.log('点击选中事件',e)
  },
  //确认
  affirmfun(){
    wx.navigateBack({
       delta: 1
    })
  },
  //日期改变时间
  bindDateChange(e){
    this.setData({
      chooseDate: e.detail.value,
      dataList: [],
    })
    _PAGE = 1;
    this.getList();
  },
  //获取列表
  getList() {
    var that = this;
    app.ajax({
      url: 'User/Pay/getFeeRecordLists',
      method: "POST",
      load: load,
      msg: '加载中...',
      data: { page: _PAGE, pageSize: _PAGESIZE, startDate: that.data.chooseDate, code_no: that.data.code},
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              dataList: res.data.data.data,
              empty: false
            });
          } else {
            that.setData({
              dataList: that.data.dataList.concat(res.data.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load = false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })

      }
    })
  },

  //展开收起更多
  showMore(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      showMoreIndex: that.data.showMoreIndex>-1 ? -1 : index,
    })
  },
})